<template>
  <div>
    <Swiper></Swiper>
    <button class="city s-city">{{ city }}</button>
    <div class="topTitle fixed" ref="T">
      <router-link to="/city"
        ><button class="city">{{ city }}</button></router-link
      >
      <p>电影</p>
    </div>
    <van-tabs v-model="activeName" sticky>
      <HotMovie></HotMovie>
      <SoonMovie></SoonMovie>
    </van-tabs>
  </div>
</template>

<script>
import Swiper from "./components/Swiper.vue";
import HotMovie from "./components/HotMovie.vue";
import SoonMovie from "./components/SoonMovie.vue";
import { getStorage } from "../../assets/js/Storage.js";
import { setStorage } from "../../assets/js/Storage.js";
import jsonpAdapter from "axios-jsonp";
export default {
  data() {
    return {
      activeName: "hotMovie",
      city: "武汉",
      nowCity: "",
    };
  },
  components: {
    Swiper,
    HotMovie,
    SoonMovie,
  },
  mounted() {
    // console.log(this.$route.path);
    if (getStorage("city")) {
      this.city = getStorage("city");
    }

    this.$http({
      url: `https://apis.map.qq.com/ws/location/v1/ip`,
      method: "get",
      params: {
        key: "23NBZ-R2B36-ZK2SU-MRIQS-KWFL6-QUFPQ",
        output: "jsonp",
      },
      adapter: jsonpAdapter,
    }).then((res) => {
      console.log(res.data.result.ad_info.city.slice(0, 2));
      this.nowCity = res.data.result.ad_info.city.slice(0, 2);
      setStorage("nowCity", this.nowCity);

      while (this.nowCity != this.city) {
        var sure = confirm("当前选择城市不为你所在的城市");
        if(sure){
          this.nowCity = res.data.result.ad_info.city.slice(0, 2);
        }else{
          this.city = this.nowCity
          return
        }
        return
      }
    });

    if (this.$route.path == "/films/soon") {
      this.activeName = "soonMovie";
    }
    window.addEventListener("scroll", this.handleScroll);
    this.$refs.T.style.opacity = 0;
    // console.log(this);
    // console.log(window);
  },
  beforeDestroy() {
    window.removeEventListener("scroll", this.handleScroll);
  },
  methods: {
    handleScroll() {
      var scrollTop = window.scrollY;
      if (scrollTop > 400) {
        this.$refs.T.style.opacity = 1;
      } else {
        this.$refs.T.style.opacity = 0;
      }
    },
  },
};
</script>

<style>
.van-tabs__wrap {
  height: 1rem !important;
}
.van-tab__text {
  font-size: 0.35rem !important;
  overflow: unset;
}
.van-tabs__content {
  background-color: #fff;
}
.movieItem {
  padding: 20px;
  display: flex;
  justify-content: space-between;
}
.movieItem img {
  width: 1.5rem;
}
.itemLeft {
  display: flex;
}
.movieContent {
  margin-left: 0.4rem;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: flex-start;
  color: #666;
}
.movieContent p {
  margin: 0.1rem 0;
}
.movieContent .title {
  color: #333;
  font-size: 0.5rem;
}
.rating {
  color: #ffb232;
}
.buy {
  color: #ffb232;
  border: 1px solid #ffb232;
  width: 1rem;
  height: 0.7rem;
}
.wantbuy {
  display: flex;
  align-items: center;
}
.fixed {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
}
.topTitle {
  height: 1rem;
  text-align: center;
  line-height: 1rem;
  font-size: 0.4rem;
  background: #fff;
  z-index: 100;
  /* position: relative; */
}
.van-sticky--fixed {
  top: 1rem;
}
.city {
  position: absolute;
  left: 0.2rem;
  top: 0.2rem;
  font-size: 0.4rem;
  height: 0.7rem;
  text-align: center;
  line-height: 0.7rem;
  border-radius: 0.2rem;
  background-color: transparent;
  border: none;
}
.s-city {
  background-color: rgba(136, 133, 133, 0.582);
  color: #fff;
}
</style>